// Copyright 2007 Google Inc.
// All Rights Reserved.

package com.google.jessewilson.codesearch.gtagssearch;

import com.google.inject.Inject;
import com.google.jessewilson.codesearch.api.SearchProvider;
import com.google.jessewilson.codesearch.api.spi.SearchProviderParameters;
import com.google.jessewilson.codesearch.api.spi.SearchProviderParametersEditor;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;

/**
 * UI for Gtags-specific query options, to be displayed in the preflight
 * query editor.
 *
 * @see GtagsSearchProviderParameters
 * @see com.google.jessewilson.codesearch.ui.preflight.PreflightQueryEditor
 * 
 * @author Ted Mao (tmao@google.com)
 */
class GtagsSearchProviderParametersEditor
    implements SearchProviderParametersEditor {

  private final JPanel panel;

  private final JRadioButton findDefinitions;
  private final JRadioButton findInvocations;

  @Inject
  public GtagsSearchProviderParametersEditor() {
    FormLayout formLayout = new FormLayout(
        "right:min, left:pref:grow",
        "min, min, 3px");
    panel = new JPanel(formLayout);

    CellConstraints cc = new CellConstraints();

    findDefinitions = new JRadioButton("Find definitions");
    findInvocations = new JRadioButton("Find invocations");
    ButtonGroup searchTypeButtons = new ButtonGroup();
    searchTypeButtons.add(findDefinitions);
    searchTypeButtons.add(findInvocations);

    // just select "find definitions" by default
    findDefinitions.setSelected(true);

    panel.add(findDefinitions, cc.xywh(1, 1, 2, 1));
    panel.add(findInvocations, cc.xywh(1, 2, 2, 1));
  }

  public SearchProvider getSearchProvider() {
    return new GtagsSearchProvider();
  }

  public void setAction(Action action) {
    // do nothing
  }

  public SearchProviderParameters getValue() {
    return new GtagsSearchProviderParameters(findInvocations.isSelected());
  }

  public void setValue(SearchProviderParameters value) {
    GtagsSearchProviderParameters parameters =
        (GtagsSearchProviderParameters) value;
    if (parameters.findInvocations()) {
      findInvocations.setSelected(true);
    } else {
      findDefinitions.setSelected(true);
    }
  }

  public JComponent getComponent() {
    return panel;
  }
}
